<template>
<div class="app-container calendar-list-container addUserPhone">
    <el-form :model="addPhoneForm" :rules="rules" ref="addPhoneForm" label-width="100px">
      <el-form-item label="手机号" prop="newPhone">
        <el-input size="small" v-model="addPhoneForm.newPhone" auto-complete="off" placeholder="请输入手机号">
          <i slot="prefix" class="icon-shouji"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input size="small" v-model="addPhoneForm.code" auto-complete="off" placeholder="请输入验证码">
          <template slot="append">
            <span @click="handleSend" class="msg-text" :class="[{display:msgKey}]">{{msgText}}</span>
          </template>
          <i slot="prefix" class="icon-yanzhengma" style="margin-top:10px;"></i>
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="uedit-footer">
      <el-button @click="cancelPhone('addPhoneForm')">取 消</el-button>
      <el-button type="primary" @click="addUserPhone('addPhoneForm')">确 定</el-button>
    </div>
</div>
</template>

<script>
const MSGINIT = '发送验证码'
const MSGSCUCCESS = '${time}秒后重发'
const MSGTIME = 60
import { sendSmsCode, addUserPhone } from '@/api/admin/user/index'
import { isvalidatemobile } from '@/utils/validate'
import { mapGetters } from 'vuex'
import {
  Message
} from 'element-ui'
export default {
  name: 'user',
  data() {
    const validatePhone = (rule, value, callback) => {
      if (isvalidatemobile(value)[0]) {
        callback(new Error(isvalidatemobile(value)[1]))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length !== 6) {
        callback(new Error('请输入6位数的验证码'))
      } else {
        callback()
      }
    }
    return {
      msgText: MSGINIT,
      msgTime: MSGTIME,
      msgKey: false,
      addPhoneForm: {
        userId: this.$store.state.user.userId,
        phone: this.$store.state.user.userphone,
        code: undefined,
        newPhone: undefined
      },
      rules: {
        newPhone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      }

    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'userType',
      'userphone',
      'platformId'
    ])
  },
  methods: {
    handleSend() {
      console.log('获取短信验证码')
      if (this.msgKey) return
      if (!this.addPhoneForm.newPhone) {
        this.$message.error('请输入手机号码')
      } else if (!/^1[34578]\d{9}$/.test(this.addPhoneForm.newPhone)) {
        this.$message.error('手机号格式不正确')
      } else {
        var str = {}
        str.phone = this.addPhoneForm.newPhone
        str.busType = '04'
        sendSmsCode(str)
          .then(response => {
            if (response.code !== '0000') {
              Message({
                message: response.message,
                type: 'warning'
              })
            } else {
              Message({
                message: '验证码发送成功',
                type: 'success'
              })
            }
          })
      }
      this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
      this.msgKey = true
      const time = setInterval(() => {
        this.msgTime--
        this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
        if (this.msgTime === 0) {
          this.msgTime = MSGTIME
          this.msgText = MSGINIT
          this.msgKey = false
          clearInterval(time)
        }
      }, 1000)
    },
    cancelPhone(formName) {
      this.resetTemp()
    },
    addUserPhone(formName) {
      var that = this
      this.$refs.addPhoneForm.validate(valid => {
        if (valid) {
          addUserPhone(this.addPhoneForm)
            .then(response => {
              if (response.code === '0000') {
                that.$store.commit('SET_PHONE', this.addPhoneForm.newPhone)
                this.$notify({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                })
                this.$router.push('/')
              } else {
                this.$notify({
                  title: '失败',
                  message: response.msg,
                  type: 'success',
                  duration: 2000
                })
              }
            })
        }
      })
    },
    resetTemp() {
      this.updatePhoneForm = {
        userId: undefined,
        phone: undefined,
        code: undefined,
        newPhone: undefined
      }
    }

  }
}
</script>
<style>
.addUserPhone{
  width: 500px;
}
.uedit-footer{
  margin-left: 100px;
}
</style>
